﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace HDJ.Framework.Core
{
    public class AssetsPathController
    {
        private static Dictionary<string, string> pathDataDic = new Dictionary<string, string>();
        private static bool isInit;
        public const string PathFileName = "PathFile";

        #region 资源路径文件处理

        private static void InitPathFile()
        {
            if (isInit)
                return;
            isInit = true;

            pathDataDic.Clear();
            if (ResourceManager.LoadType== AssetsLoadType.Resources)
            {
                TextAsset textAss = Resources.Load<TextAsset>(PathFileName);
                pathDataDic = DealWithPathFileData(textAss.text);
            }
            else
            {
                string platformFolderName = UnityExtendUtils.GetPlatformFolderName(Application.platform);
                string tempPath = platformFolderName + "/" + PathFileName;

                //加载StreamingAssets文件夹下的路径总文件
                string filePathStream = PathUtils.GetSpecialPath(tempPath + ".assetbundle", SpecialPathType.StreamingAssets);
                AssetBundle assetBundle = AssetBundle.LoadFromFile(filePathStream);
                TextAsset sPathFile=  (TextAsset)assetBundle.LoadAllAssets()[0];
                string resArr = sPathFile.text;

                pathDataDic = DealWithPathFileData(resArr);
                List<string> list = new List<string>(pathDataDic.Keys);
                for (int i = 0; i < list.Count; i++)
                {
                    pathDataDic[list[i]] = PathUtils.GetSpecialPath(platformFolderName + "/" + pathDataDic[list[i]], SpecialPathType.StreamingAssets);
                }

                //加载沙河路径下热跟新下来的的路径总文件，并且和StreamingAssets文件夹下的路径总文件合并
                string filePathPer = PathUtils.GetSpecialPath(tempPath + ".txt", SpecialPathType.Persistent);
                if (File.Exists(filePathPer))
                {
                    string textDataPer = FileUtils.LoadTextFileByPath(filePathPer);
                    Dictionary<string, string> pathDataPer = DealWithPathFileData(textDataPer);
                    foreach (string key in pathDataPer.Keys)
                    {
                        string path = PathUtils.GetSpecialPath(platformFolderName + "/" + pathDataPer[key], SpecialPathType.Persistent);
                        if (pathDataDic.ContainsKey(key))
                        {
                            pathDataDic[key] = path;
                        }
                        else
                        {
                            pathDataDic.Add(key, path);
                        }
                    }
                }

            }
        }
        public static Dictionary<string, string> DealWithPathFileData(string textData)
        {
            Dictionary<string, string> temp = JsonUtils.FromJson<Dictionary<string, string>>(textData);
            return temp;
        }
        public static List<string> GetAllPathNames()
        {
            return new List<string>(pathDataDic.Keys);
        }
        public static string GetPath(string resName)
        {
            InitPathFile();
            resName = resName.ToLower();

            if (!pathDataDic.ContainsKey(resName))
            {
                Debug.LogError("没有文件：" + resName);
                return "";
            }
            return pathDataDic[resName];
        }

        internal static void Release(string name)
        {
            throw new NotImplementedException();
        }

        public static void ClearPathData()
        {
            isInit = false;
            pathDataDic.Clear();
        }
        /// <summary>
        /// 资源中是否存在该名字的资源，忽略大小写
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool ExistFile(string name)
        {
            InitPathFile();
            name = name.ToLower();
            return pathDataDic.ContainsKey(name);
        }



        #endregion
    }
}
